package com.zhiyizhen.handheld.component.execute.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.execute.bean.executedetail.ExecuteDetailBean;
import com.zhiyizhen.handheld.component.execute.bean.executedetail.GroupListItem;
import com.zhiyizhen.handheld.databinding.ItemShuyeBinding;

public class ExecuteDetailShuyeAdapter extends BaseAdapter<ExecuteDetailBean.RxInfoBean, ExecuteDetailShuyeAdapter.ExecuteDetailShuyeViewHolder> {
    private OnActionClickListener onActionClickListener;
    private Context context;

    public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    public ExecuteDetailShuyeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @NonNull
    @Override
    public ExecuteDetailShuyeAdapter.ExecuteDetailShuyeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ExecuteDetailShuyeAdapter.ExecuteDetailShuyeViewHolder(this, ItemShuyeBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ExecuteDetailShuyeViewHolder extends BaseViewHolder<ExecuteDetailBean.RxInfoBean> {

        private final ItemShuyeBinding binding;

        public ExecuteDetailShuyeViewHolder(BaseAdapter<ExecuteDetailBean.RxInfoBean, ExecuteDetailShuyeAdapter.ExecuteDetailShuyeViewHolder> adapter, ItemShuyeBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(ExecuteDetailBean.RxInfoBean data) {
            binding.injectionBtn.setOnClickListener(v -> {
                if (onActionClickListener != null) {
                    onActionClickListener.onAddCharge(data);
                }
            });
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            binding.recyclerView.setLayoutManager(layoutManager);
            ExecuteShuyeGroupAdapter adapter = new ExecuteShuyeGroupAdapter(context);
            adapter.setOnActionClickListener(data1 -> {
                if (onActionClickListener != null) {
                    onActionClickListener.onAddExecuteRecord(data1);
                }
            });
            binding.recyclerView.setAdapter(adapter);
            adapter.setData(data.getGroupList());
        }
    }

    public interface OnActionClickListener {
        /**
         * 添加注射费
         */
        void onAddCharge(ExecuteDetailBean.RxInfoBean data);

        /**
         * 添加执行记录
         */
        void onAddExecuteRecord(GroupListItem data);
    }
}